


pub mod model;
mod dao;
mod service;
pub mod controller;


use axum::Router;
use axum::routing::{get, post};

/* router.nest("/article", config_router()) */
pub fn config_router() -> Router
{
    Router::new()
        .route("/get/:id", get(controller::get_article_by_id))
        .route("/delete/:id", get(controller::delete_article_by_id))
        .route("/upload", post(controller::upload_article))
        .route("/update", post(controller::update_article))



}


#[cfg(test)]
mod tests
{
    use super::*;
    use crate::test;

    async fn app() -> Router
    {
        let mut map = ::std::collections::HashMap::<&str, Router>::new();
        map.insert("/user", crate::user::config_router());
        map.insert("/article", config_router());
        test::init_app_test(
            Option::None,
            map
        ).await
    }

    async fn login(server: &axum_test::TestServer)
    {
        let user = crate::user::model::User::new(0, "weifei", "weifei", None);
        let response = server
            .post("/user/login")
            .json(&user)
            .await;
        println!("{}", response.text());
    }

    #[tokio::test]
    async fn test_get_article_by_id()
    {
        let response = test::get(app().await, "/article/get/2").await;
        println!("{}", response.text());
    }

    #[tokio::test]
    async fn test_delete()
    {
        let server = test::start_server_test(app().await);
        login(&server).await;// 登录

        let response = server
            .get("/article/delete/2")
            .await;
        println!("{}", response.text());
    }

    #[tokio::test]
    async fn test_upload()
    {
        let article = model::TestArticle {
            id: 0,
            author_uid: 1,
            title: Some("测试文章8".to_string()),
            upload_time: rbatis::rbdc::datetime::DateTime::now(),
            update_time: rbatis::rbdc::datetime::DateTime::now(),
            text: Some("# 测试，测试，还是tm的测试！".to_string()),
            sorts: None,
            tags: None
        };

        let server = test::start_server_test(app().await);
        login(&server).await;// 登录
        let response = server
            .post("/article/upload")
            .json(&article)
            .await;

        println!("{}", response.text());
    }


    #[tokio::test]
    async fn test_update()
    {
        let article = model::TestArticle {
            id: 7,
            author_uid: 1,
            title: Some("测试文章7，测试update".to_string()),
            upload_time: rbatis::rbdc::datetime::DateTime::now(),
            update_time: rbatis::rbdc::datetime::DateTime::now(),
            text: Some("# 测试update".to_string()),
            sorts: None,
            tags: None
        };

        let server = test::start_server_test(app().await);
        login(&server).await;// 登录
        let response = server
            .post("/article/update")
            .json(&article)
            .await;
        println!("{}", response.text());
    }
}


